<?php

class PecahAyat
{
    protected $pdo;
    
    public function __construct(\PDO $pdo = null)
    {
        if ($pdo == null) {
            $dsn      = 'mysql:dbname=qas_quran; host=localhost; port=3306';
            $username = 'root';
            $password = 'kosong';
            $options  = array(
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
            ); 

            try {
                $pdo  = new \PDO($dsn, $username, $password, $options);
                $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
                $this->pdo = $pdo;
            } catch (\PDOException $e) {
                ?>
                <div class="row">
                    <div class="col-lg-12">
                        <h1 class="page-header">ERROR</h1>
                    </div>
                </div>
                <?php
                echo '<div class="alert alert-danger">Connection failed : ' . $e->getMessage().'</div>';
                exit;
            }
        } else {
            $this->pdo = $pdo;
        }
    }
    
    public function pecahAyatToKata()
    {
        $sql = "SELECT * FROM `quran_text_indonesia` WHERE sura>67";
        //~ $sql = "SELECT * FROM `quran_text_indonesia`";
        //~ $sql = "SELECT * FROM `quran_text_indonesia` WHERE sura=114";
        $no = 1;
        $result = array();
        foreach ($this->pdo->query($sql) as $row) {
            echo $row['sura'].' - '.$row['aya'].' - '.$row['text'].'<br/>';
            
            $preProcessingService = new PreProcessing($this->pdo);
            $hasilStemming = $preProcessingService->stemming($row['text']);
            $pecahHasilStemming = explode(" ", $hasilStemming);
            foreach ($pecahHasilStemming as $kata) {
                echo $kata.'<br/>';
                $result[$row['sura']][$row['aya']][$kata] = $kata;
                //~ $sql = "INSERT INTO `daftar_kata_quran` (sura, aya, kata) VALUES (:sura, :aya, :kata)";
                //~ $q   = $this->pdo->prepare($sql);
                //~ $q->execute(
                    //~ array(
                        //~ ':sura'  => strtolower($row['sura']),
                        //~ ':aya'   => strtolower($row['aya']),
                        //~ ':kata'  => strtolower($kata),
                    //~ )
                //~ );
            }
            echo '<br/>';
        }
        
        foreach ($result as $sura => $dataSura) {
            foreach ($dataSura as $aya => $dataAya) {
                foreach ($dataAya as $kata) {
                    $sql = "INSERT INTO `daftar_kata_quran` (sura, aya, kata) VALUES (:sura, :aya, :kata)";
                    $q   = $this->pdo->prepare($sql);
                    $q->execute(
                        array(
                            ':sura'  => strtolower($sura),
                            ':aya'   => strtolower($aya),
                            ':kata'  => strtolower($kata),
                        )
                    );
                }
            }
        }
    }
    
    public function updateDaftarKataQuran()
    {
        $sql = "SELECT * FROM `daftar_kata_quran` WHERE jenis_kata=''";
        $no = 1;
        $daftarKata = array();
        foreach ($this->pdo->query($sql) as $row) {
            $daftarKata[$row['kata']] = $row['kata'];
            echo $row['sura'].' - '.$row['aya'].' - '.$row['kata'].'<br/>';
        }
        
        if (count($daftarKata) > 0) {
            $listKata = implode("', '", $daftarKata);
            $sqlKataDasar = "SELECT * FROM `kata_dasar` WHERE kata IN ('".$listKata."')";
            $daftarKataDasar = array();
            foreach ($this->pdo->query($sqlKataDasar) as $row) {
                $daftarKataDasar[$row['kata']] = $row['tipe'];
            }
            
            foreach ($this->pdo->query($sql) as $row) {
                $jenisKata = '';
                if (isset($daftarKataDasar[$row['kata']])) {
                    $jenisKata = strtolower($daftarKataDasar[$row['kata']]);
                }
                
                $sql = "UPDATE `daftar_kata_quran` SET `jenis_kata`=? WHERE kata='".$row['kata']."' AND sura='".$row['sura']."' AND aya='".$row['aya']."'";
                $q = $this->pdo->prepare($sql);
                $q->execute(array($jenisKata));
            }
        }
    }
}

